<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/22
 * Time: 9:47
 *订单管理
 */

require('../include/init.inc.php');
require('../../../order_store_procedure.php');
$method = $page_no = '';
extract($_REQUEST, EXTR_IF_EXISTS);


//订单管理 只能管理本服的 查询是查询所有的订单
//$db = createTemTable();
//$allData = getAllData();
//$sql_str = '';
//$sign = ',';
//foreach ($allData as $website => $data) {
//    if (!$data) {
//        unset($allData[$website]);
//    }
//}
//
//foreach ($allData as $website => $data) {
//    if ($data) {
//        foreach ($data as $item) {
//
//            $ws = "'" . $website . "'";
//            $order_sn = "'" . $item['order_sn'] . "'";
//            $userid = "'" . $item['userid'] . "'";
//            $shipping_method = "'" . $item['shipping_method'] . "'";
//            $pay_type = "'" . $item['pay_type'] . "'";
//            $user_name = "'" . $item['user_name'] . "'";
//            $shipping_time = "'" . $item['shipping_time'] . "'";
//            $phone = "'" . $item['phone'] . "'";
//            $line = "'" . $item['line'] . "'";
//            $addres = "'" . $item['addres'] . "'";
//            $gender = "'" . $item['gender'] . "'";
//            $email = "'" . $item['email'] . "'";
//            $note = "'" . $item['note'] . "'";
//            $status = "'" . $item['status'] . "'";
//            $createtime = "'" . $item['createtime'] . "'";
//            $modifytime = "'" . $item['modifytime'] . "'";
//            $value_str_s = '(' . $ws . ',' . $order_sn . ',' . $userid . ',' . $shipping_method . ',' . $pay_type . ',' . $user_name . ',' . $shipping_time . ',' . $phone . ',' . $line . ',' . $addres . ',' . $gender . ',' . $email . ',' . $note . ',' . $status . ',' . $createtime . ',' . $modifytime . ')';
//            $sql_str .= $value_str_s . $sign;
//        }
//    }
//}
//
//
//$sql_str = substr($sql_str, 0, strlen($sql_str) - 1);
//
//$sql_exec_str = "replace into `t_tmp_orders` (`web_site`,`order_sn`,`userid`,`shipping_method`,`pay_type`,`user_name`,`shipping_time`,`phone`,`line`,`addres`,`gender`,`email`,`note`,`status`,`createtime`,`modifytime`) values $sql_str";
//
//$dumpRet = dumpDataToTmp($sql_exec_str);

$today_condition = " WHERE `status` = 0 AND FROM_UNIXTIME(createtime,'%Y-%m-%d') = CURDATE()";//查出今天的订单量
$today_order_nums = AdminOrder::getCountBySearch($today_condition);


$search = array("status" => "0");
$row_count = AdminOrder::getCountBySearch($search);
$page_size = PAGE_SIZE;
$page_no = $page_no < 1 ? 1 : $page_no;

$total_page = $row_count % $page_size == 0 ? $row_count / $page_size : ceil($row_count / $page_size);
$total_page = $total_page < 1 ? 1 : $total_page;
$page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
$start = ($page_no - 1) * $page_size;


$search_o = " `status` = 0 ";
$orders = AdminOrder::getAllOrders($start, $page_size, $search_o);

foreach ($orders as $ord) {
    $shop_cart = AdminShopCart::getShopCartBySn($ord['order_sn']);
//    foreach ($shop_cart as $sc) {
//        $newn_order['num'] = $sc['product_nums'];
//        $newn_order['products_color'] = $sc['color'];
//        $newn_order['products_size'] = $sc['size'];
//
//        $product = AdminProducts::getProductById($sc['product_id']);
//        $newn_order['total_price'] = $sc['product_nums'] * $product['products_special_price'];
//        $newn_order['products_name'] = $product['products_name'];
//        $newn_order['products_model'] = $product['products_model'];
//        $new_order['prod_infos'] = $newn_order;
//    }

    $new_order['prod_infos'] = $shop_cart;
    $nameslist = getBlackNameList('user_name');
    foreach ($nameslist as $n) {
        $names[] = $n['user_name'];
    }
    $phonelist = getBlackNameList('phone');
    foreach ($phonelist as $n) {
        $phones[] = $n['phone'];
    }
    $linelist = getBlackNameList('line');
    foreach ($linelist as $n) {
        $lines[] = $n['line'];
    }
    $emaillist = getBlackNameList('email');
    foreach ($emaillist as $n) {
        $emails[] = $n['email'];
    }


    if (($names && in_array($ord['user_name'], $names)) || ($phones && in_array($ord['phone'], $phones)) || ($lines && in_array($ord['line'], $lines)) || ($emails && in_array($ord['email'], $emails))) {
        $new_order['sadOrder'] = "1";
    } else {
        $new_order['sadOrder'] = "0";
    }

    $new_order['pay_type'] = Common::getValue($ord['pay_type'], 'pay_method');
    $payLog = AdminPayLog::getLogByOrderSn($ord['order_sn']);

    if ($ord['pay_type'] == '111') {
        $new_order['pay_info'] = "貨到付款用戶";
    } else {
        if (!$payLog) {
            $new_order['pay_info'] = "支付未成功，漏單用戶";
        } else {
            if (!$payLog['RetStatus'] && !$payLog['RetCode']) {
                $new_order['pay_info'] = "点击支付按钮之后关闭浏览器导致支付失败";
            } else {
                if (!$payLog['RetStatus'] && $payLog['RetStatus'] == 1) {
                    $new_order['pay_info'] = "支付成功";
                } else {
                    $new_order['pay_info'] = $payLog['RetMsg'];
                }
            }
        }
    }
    $new_order['order_sn'] = $ord['order_sn'];
    $new_order['user_name'] = $ord['user_name'];
    $new_order['phone'] = $ord['phone'];
    $new_order['addres'] = $ord['addres'];
    $new_order['note'] = $ord['note'];
    $new_order['createtime'] = date("Y-m-d H:i:s", $ord['createtime']);
    $kd = AdminKd::getKdInfoBySn($ord['order_sn']);
    $new_order['kd_name'] = $kd['kd_name'];
    if ($kd['isGo'] == 1) {
        $new_order['kd_status'] = '已发货';
    } elseif ($kd['isGo'] == 0) {
        $new_order['kd_status'] = '未发货';
    } else {
        $new_order['kd_status'] = '取消订单';
    }

    $new_orders[] = $new_order;
}

$page_html = Pagination::showPager("order.php", $page_no, PAGE_SIZE, $row_count);

Template::assign('page_no', $page_no);
Template::assign('page_size', PAGE_SIZE);
Template::assign('row_count', $row_count);
Template::assign('page_html', $page_html);

$confirm_html = OSAdmin::renderJsConfirm("icon-remove");
Template::assign('osadmin_action_confirm', $confirm_html);
Template::assign('today_order_nums', $today_order_nums);
Template::assign('orders', $new_orders);
Template::display('one_page_shop/order.tpl');